using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace sCicero
{
    
    //Todo: add exceptions and exceptionhandling and validating the input
    public partial class BaseControl : UserControl
    {
        private string m_sTitle = "";
        private ArrayList m_oButtons = new ArrayList();
        private ArrayList m_oControlsToAdd = new ArrayList();
        public BaseControl()
        {
            InitializeComponent();
        }

        public void ShowDialog()
        {
            LayoutButtons();
            SetBounds();
            this.ParentForm.Text = m_sTitle;

        }

        private void SetBounds()
        {
            //if (Visible)
            //{
                this.Height = tableLayoutPanel.Height + flowLayoutPanel.Height + 20;
                this.Width = tableLayoutPanel.Width + 20;
                //change the width and height to fit
                this.ParentForm.Width = this.Width + 10;
                this.ParentForm.Height = this.Height + 30;
                this.ParentForm.Refresh();
            //}
        }

        public void LayoutButtons()
        {
            if (m_oControlsToAdd.Count > 0)
            {
                tableLayoutPanel.ColumnStyles.Clear();
                int nColumnCount = 0;
                foreach (Control[] oCons in m_oControlsToAdd)
                {
                    if (nColumnCount < oCons.Length)
                    {
                        nColumnCount = oCons.Length;
                    }
                }
                tableLayoutPanel.ColumnCount = nColumnCount;
                for (int i = 0; i < nColumnCount; i++)
                {
                    float nF = (float)(100 / nColumnCount);
                    tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, nF));    
                }
                tableLayoutPanel.GrowStyle = TableLayoutPanelGrowStyle.AddColumns;
                foreach (Control[] oCons in m_oControlsToAdd)
                {
                    AddTableRowControl(oCons);
                    /*
                    int nCI = 0;
                    tableLayoutPanel.RowCount++;

                    foreach (Control oC in oCons)
                    {
                        tableLayoutPanel.Controls.Add(oC, nCI++, tableLayoutPanel.RowCount);
                    }
                     * */
                }
                tableLayoutPanel.RowCount++;
            }
            //add the lower buttons
            if (m_oButtons.Count > 0)
            {
                Control[] oCons = (Control[])m_oButtons.ToArray(typeof(Button));
                AddFlowTableRow(oCons);
                foreach (Button oB in m_oButtons)
                {
                    if (oB.Tag.ToString() == "cancel")
                        this.ParentForm.CancelButton = oB;

                    if (oB.Tag.ToString() == "insert")
                        this.ParentForm.AcceptButton = oB;

                }
            }
        }
        public void RemoveControl(string sControlName)
        {
            RemoveControlIter(sControlName, this);
            
        }
        public void RemoveControlIter(string sControlName, Control oControl)
        {
            bool bRemoved = false;
            foreach (Control oC in oControl.Controls)
            {
                if (oC.Controls.ContainsKey(sControlName))
                {
                    Controls.RemoveByKey(sControlName);
                    oC.Refresh();
                    bRemoved = true;
                }
                else
                    RemoveControlIter(sControlName, oC);

                if (bRemoved)
                    return;
            }
        }

        /*
        public void UpdateControl(Control oUpdCon)
        {

            tableLayoutPanel.Controls[oUpdCon.Name].con = oUpdCon;
        }
         * */
         



        public void CloseForm()
        {
            //this.ParentForm.CancelButton.PerformClick();
            //((BaseDlg)this.ParentForm).CloseForm();
        }
         

        public Control GetButtonControl()
        {
            return flowLayoutPanel;
        }
        public void SetTitle(string sTitle)
        {
            groupBox.Text = sTitle;
            m_sTitle = sTitle;
        }
        public void ClearControls()
        {
            this.Controls.Clear();
            InitializeComponent();
            m_oButtons.Clear();
            m_oControlsToAdd.Clear();
        }
        
        //add "extra" controls on the fly 
        public void AddTableRowControls(Control[] oCons)
        {
            AddTableRowControl(oCons);
        }
        /*
        //add "extra" controls on the fly at specific location (row)
        public void AddTableRowControls(Control[] oCons, bool bNewRow)
        {
            AddTableRowControl(oCons, bNewRow);
        }
         

        public void AddTableRowControls(Control[] oCons, int nRowIndex)
        {
            AddTableRowControl(oCons, nRowIndex);
        }

        public void AddTableRowControls(Control[] oCons, int nRowIndex, bool bNewRow)
        {
            AddTableRowControl(oCons, nRowIndex, bNewRow);
        }
        */

        public void SetVisible(bool bVisible)
        {
            this.Visible = bVisible;
        }

        private void AddTableRowControl(Control[] oCons, bool bNewRow)
        {
            int nCI = 0;
            tableLayoutPanel.RowCount++;
            if (oCons.Length == 1)
            {
                //place the control in the second column since there is only one control.
                tableLayoutPanel.Controls.Add(oCons[0], 1, tableLayoutPanel.RowCount);
            }
            else
            {
                foreach (Control oC in oCons)
                {
                    tableLayoutPanel.Controls.Add(oC, nCI++, bNewRow ? tableLayoutPanel.RowCount++ : tableLayoutPanel.RowCount);
                }
            }
            //if (bSetBounds)
            SetBounds();
        }

        private void AddTableRowControl(Control[] oCons)
        {
            int nCI = 0;
            tableLayoutPanel.RowCount++;
            if (oCons.Length == 1)
            {
                //place the control in the second column since there is only one control.
                tableLayoutPanel.Controls.Add(oCons[0], 1, tableLayoutPanel.RowCount);
            }
            else
            {
                foreach (Control oC in oCons)
                {
                    tableLayoutPanel.Controls.Add(oC, nCI++, tableLayoutPanel.RowCount);
                }
            }
            //if (bSetBounds)
                SetBounds();

                Update();
        }
        public int GetLastRowIndex()
        {
            return tableLayoutPanel.RowCount;
        }
        public void ClearControlsAtRow(int nRowIndex)
        {
            for (int nRCount = 0; nRCount < tableLayoutPanel.ColumnCount; nRCount++)
			{
                if (tableLayoutPanel.GetControlFromPosition(nRCount, nRowIndex) != null)
                    tableLayoutPanel.Controls.Remove(tableLayoutPanel.GetControlFromPosition(nRCount, nRowIndex));
			}

            
            //foreach (tableLayoutPanel)
            
        }

        private void AddTableRowControl(Control[] oCons, int nRowIndex, bool bNewRow)
        {
            int nCI = 0;
            if (oCons.Length == 1)
            {
                //place the control in the second column since there is only one control.
                tableLayoutPanel.Controls.Add(oCons[0], 1, nRowIndex);
            }
            else
            {
                foreach (Control oC in oCons)
                {
                    tableLayoutPanel.Controls.Add(oC, nCI++, bNewRow ? nRowIndex++ : nRowIndex);
                }
            }
            //if (bSetBounds)
            SetBounds();
        }

        private void AddTableRowControl(Control[] oCons, int nRowIndex)
        {
            int nCI = 0;
            if (oCons.Length == 1)
            {
                //place the control in the second column since there is only one control.
                tableLayoutPanel.Controls.Add(oCons[0], 1, nRowIndex);
            }
            else
            {
                foreach (Control oC in oCons)
                {
                    tableLayoutPanel.Controls.Add(oC, nCI++, nRowIndex);
                }
            }
            //if (bSetBounds)
            SetBounds();
        }

        public void AddTableRow(Control[] oCons)
        {
            m_oControlsToAdd.Add(oCons);
        }

        public void AddFlowTableRow(Control[] oCons)
        {
            flowLayoutPanel.Controls.AddRange(oCons);
        }

        public void AddButton(Control oButton)
        {
            //just stack up the buttons. Layout them last
            m_oButtons.Add(oButton);
        }
    }
}
